<template>
  <a-modal
    :title="model && model.id > 0 ? '编辑店面' : '新增店面'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item v-show="false" label="ID">
          <a-input v-decorator="['id', { initialValue: model?.id }]" disabled />
        </a-form-item>
        <a-form-item label="店名">
          <a-input
            v-decorator="[
              'storeName',
              {
                rules: [
                  { required: true, message: '请输入店名' },
                ],
              },
            ]"
            placeholder="请输入店名"
            name="name"
          />
        </a-form-item>
        <a-form-item label="联系电话">
          <a-input
            v-decorator="[
              'phone',
              {
                initialValue: model?.phone,
                rules: [
                  { required: true, message: '请输入手机号' },
                  { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' },
                ],
              },
            ]"
            placeholder="请输入联系电话"
            name="phone"
          />
        </a-form-item>
        <a-form-item label="省">
          <a-input v-decorator="['province', { initialValue: model?.province }]" placeholder="请输入省" name="idcard" />
        </a-form-item>
        <a-form-item label="市">
          <a-input v-decorator="['city', { initialValue: model?.city }]" placeholder="请输入市" name="time" />
        </a-form-item>
        <a-form-item label="地址">
          <a-input v-decorator="['address', { initialValue: model?.address }]" placeholder="请输入地址" name="dian" />
        </a-form-item>
        <a-form-item label="状态" v-show="model && model.id > 0">
          <a-select v-decorator="['storeStatus', { initialValue: model?.storeStatus }]" placeholder="请选择">
            <a-select-option value="1">营业中</a-select-option>
            <a-select-option value="2">已停业</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
// 表单字段
const fields = ['storeName', 'phone', 'address', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      form: this.$form.createForm(this),
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
}
</script>
